package com.cskaoyan.service;

import com.cskaoyan.bean.CskaoyanmallStorage;
import com.cskaoyan.bean.CskaoyanmallStorageExample;

import com.cskaoyan.mapper.CskaoyanmallStorageMapper;
import com.cskaoyan.vo.StorageVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CskaoyanmallStorageServiceImpl implements CskaoyanmallStorageService {

    @Resource
    private CskaoyanmallStorageMapper cskaoyanmallStorageMapper;

    @Override
    public long countByExample(CskaoyanmallStorageExample example) {
        return cskaoyanmallStorageMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(CskaoyanmallStorageExample example) {
        return cskaoyanmallStorageMapper.deleteByExample(example);
    }

    @Override
    public List<CskaoyanmallStorage> selectByExample(CskaoyanmallStorageExample example) {
        return cskaoyanmallStorageMapper.selectByExample(example);
    }

    @Override
    public int updateByExampleSelective(CskaoyanmallStorage record, CskaoyanmallStorageExample example) {
        return cskaoyanmallStorageMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(CskaoyanmallStorage record, CskaoyanmallStorageExample example) {
        return cskaoyanmallStorageMapper.updateByExample(record, example);
    }

    @Override
    public int insert(CskaoyanmallStorage record) {
        return cskaoyanmallStorageMapper.insert(record);
    }

    @Override
    public int insertSelective(CskaoyanmallStorage record) {
        return cskaoyanmallStorageMapper.insertSelective(record);
    }

    @Override
    public CskaoyanmallStorage selectByPrimaryKey(Integer id) {
        return cskaoyanmallStorageMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(CskaoyanmallStorage record) {
        return cskaoyanmallStorageMapper.updateByPrimaryKey(record);
    }

    @Override
    public StorageVo query(Integer page, Integer limit, String name, String key, String sort, String order) {
        //分页和排序
        //页码、数据量
        PageHelper.startPage(page, limit); //mybatis在构建sql语句 👉 会构造limit和offset

        CskaoyanmallStorageExample adExample = new CskaoyanmallStorageExample();
        adExample.setOrderByClause(sort + " " + order);//${}做Sql拼接
        CskaoyanmallStorageExample.Criteria criteria = adExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (name != null && !"".equals(name)){
            criteria.andNameLike("%" + name +"%");
        }
        if (key !=null && !"".equals(key)){
            criteria.andKeyLike("%"+key +"%");
        }
        List<CskaoyanmallStorage> ads = cskaoyanmallStorageMapper.selectByExample(adExample);
        //total !=comment.size();
        //total是满足条件的所有的数据量 👉 而不是当前的limit
        //可以通过PageInfo获得分页信息
        PageInfo<CskaoyanmallStorage> userPageInfo = new PageInfo<>(ads);//查询结果放进来
        long total = userPageInfo.getTotal();//总的数据量 👉 select count(*) 条件是byExample


        StorageVo storageVo = new StorageVo();
        storageVo.setItems(ads);
        storageVo.setTotal(total);
        return storageVo;
    }


    @Override
    public int delete(CskaoyanmallStorage cskaoyanmallStorage) {
        cskaoyanmallStorage.setDeleted(true);
        int i = updateByPrimaryKey(cskaoyanmallStorage);
        if (i == 1) {
            return 0;
        }else {
            return 400;
        }
    }



}

